package com.ioidea.ble.proto

import com.ioidea.base.util.ByteUtils
import com.ioidea.base.util.MyLogger
import com.ioidea.base.util.Symbol
import java.lang.Exception
import java.util.Locale

/**
3.4 PLC单控/组控 DIMALL3
数据： 亮度1 亮度2 亮度3  SN(十六进制) 所属组ID 控制类型
示例：手机->设备
DIMALL3 00 11 00 11 00 11 11 22 33 44 55 66 01 06
 */
class ProtoSingleLightControl(
    override var requestCommand: String = "DIMALL3",
    override var responseCommand: String = "DIMALL3"
) : ProtoBase() {


    companion object {
        //进入单控
        const val SINGLE_CONTROL_IN = 4

        //退出单控
        const val SINGLE_CONTROL_OUT = 5

        //进入组控
        const val GROUP_CONTROL_IN = 6  //暂时不存在组控 组控制目前是策略

        //退出组控
        const val GROUP_CONTROL_OUT = 7
    }


//    fun parseSingleControlResponse(wholeData: String): Int {
//        //去头尾
//        val data = removeHeadTail(wholeData)
//        MyLogger.hLog().i("data:$data")
//        if (wholeData.contains(head)) {
//            try {
//                return data.toInt()
//            } catch (e: Exception) {
//                MyLogger.hLog().e(e.message)
//            }
//            return -1
//        }
//        return 0
//    }

    /**
     * @param lightSn 灯的sn
     * @param groupId 组id
     * @param controlType  控制类型：4,进入单控 5，退出单控 6，进入组控 7，退出组控
     * @param brightness1  百分比的数值  真正传值要改为真实亮度值的四位16进制数
     * @param brightness2
     * @param brightness3
     */
    fun getPayloadByteData(
        lightSn: String,
        groupId: Int,
        controlType: Int,
        brightness1: Int,
        brightness2: Int,
        brightness3: Int = 0
    ): MutableList<Byte> {
        val dataList = mutableListOf<Byte>()
        val brightness1ByteArray = ByteUtils.to2Bytes(brightness1 * 10)
        val brightness2ByteArray = ByteUtils.to2Bytes(brightness2 * 10)
        val brightness3ByteArray = ByteUtils.to2Bytes(brightness3 * 10)
        // 亮度1
        dataList.addAll(brightness1ByteArray.toList())
        // 亮度2
        dataList.addAll(brightness2ByteArray.toList())
        // 亮度3
        dataList.addAll(brightness3ByteArray.toList())
        // SN
        val snSpaceStr = ProtoStrHelper.getStringAddSpace(lightSn).uppercase(Locale.getDefault())
        dataList.addAll(ByteUtils.hexToBytes(snSpaceStr).toList())
        // 组id
        dataList.add(ByteUtils.to2Bytes(groupId)[1])
        // 控制类型
        dataList.add(ByteUtils.to2Bytes(controlType)[1])
        return dataList
    }
}